<?php
namespace app\common\model;
use think\Model;

/**
 * 服务相关
 */
class Service extends Model {
	protected $insert = ['add_time'];

    protected function setAddTimeAttr(){
        return time();
    }

    /**
     * [serviceByMap 地图服务列表]
     * @return [type] [description]
     */
    public function serviceByMap($where = [],$lng = '',$lat = '',$having = ''){
        $list = $this->alias('s')->join(['bz_user' => 'u'],'u.id = s.user_id')->join(['bz_user_position' => 'p'],'p.user_id = u.id')
            ->where('s.status',1)->where($where)
            ->field("s.id,u.id as user_id,nick_name,photo_path,age,sex,zhiye,kaopu,service_count,p.lng,p.lat,ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN(( $lat * PI() / 180 - lat * PI() / 180) / 2),2) + COS($lat * PI() / 180) * COS(lat * PI() / 180) * POW(SIN(($lng * PI() / 180 - lng * PI() / 180) / 2),2)))) AS juli")
            ->select();

        $zhiye = db('zhiye');
        foreach($list as $key => $val){
            $list[$key]['zhiye'] = $val['zhiye'] == 0?'':$zhiye->where('id',$val['zhiye'])->value('name');
        }
        return empty($list)?[]:$list;
    }


    /**
     * [serviceList 服务列表]
     * @param  array   $where [description]
     * @param  string  $order [description]
     * @param  integer $p     [description]
     * @param  string  $lng   [description]
     * @param  string  $lat   [description]
     * @return [type]         [description]
     */
    public function serviceList($where = [] ,$order = '',$p = 0, $lng = '', $lat = '',$page_size = 0){
        if($page_size == 0){
            $page_size = config('page_size');
        }
        $field = 's.*,u.nick_name,photo_path,zhiye,sex,kaopu,service_count,u.tag as user_tag';
        if($lng !== '' && $lat !== ''){
            $field .= ",ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN(( $lat * PI() / 180 - from_lat * PI() / 180) / 2),2) + COS($lat * PI() / 180) * COS(from_lat * PI() / 180) * POW(SIN(($lng * PI() / 180 - from_lng * PI() / 180) / 2),2)))) AS juli";
        }

        $this->alias('s')->join(['bz_user' => 'u'],'s.user_id = u.id')
            ->where($where)->where('s.status',1)
            ->order($order);
        if($p > 0){
            $this->limit(($p-1)*$page_size,$page_size);
        }
            
        $list = $this->field($field)
            ->select();

        return  $list;
    }

    //数据操作
    public function dataCheck($list = [], $user_id = 0){
        $zhiyeModel = db('zhiye');
        foreach($list as $key => $val){
            $list[$key]['image'] = empty($val['image']) ? [] : explode(',',$val['image']);
            $list[$key]['tag'] = $val['tag'] == '' ? [] : explode(',',$val['tag']);
            $list[$key]['user_tag'] = $val['user_tag'] == '' ? [] : explode(',',$val['user_tag']);
            $list[$key]['format_time'] = timeFormat($val['add_time']);
            
            if(isset($val['photo_path']) && $val['photo_path'] && strpos($val['photo_path'],'http') === false){
                $list[$key]['photo_path'] = config('http_server').$val['photo_path'];
            }

            //职业
            if($val['zhiye']){
                $list[$key]['zhiye'] = $zhiyeModel->where('id',$val['zhiye'])->value('name');
            }else{
                $list[$key]['zhiye'] = '';
            }
        }

        return $list;
    }
    

    /**
     * [getInfo 详情]
     * @param  array  $where [description]
     * @param  string $lng   [description]
     * @param  string $lat   [description]
     * @return [type]        [description]
     */
    public function getInfo($where = [],$lng = '',$lat = ''){

        $info = $this->alias('s')->join(['bz_user' => 'u'],'u.id = s.user_id')
            ->where($where)->where('s.status',1)
            ->field("s.*,zhiye,u.nick_name,photo_path,kaopu,service_count,sex,u.tag as user_tag,ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN(( $lat * PI() / 180 - from_lat * PI() / 180) / 2),2) + COS($lat * PI() / 180) * COS(from_lat * PI() / 180) * POW(SIN(($lng * PI() / 180 - from_lng * PI() / 180) / 2),2)))) AS juli")
            ->find();

        if(is_null($info)){
            return $info;
        } 
        
        $info['image'] = empty($info['image']) ? [] : explode(',',$info['image']);
        $info['tag'] = $info['tag'] == '' ? [] : explode(',',$info['tag']);
        $info['user_tag'] = $info['user_tag'] == '' ? [] : explode(',',$info['user_tag']);
        $info['format_time'] = timeFormat($info['add_time']);
        if($info['zhiye']){
            $info['zhiye'] = db('zhiye')->where('id',$info['zhiye'])->value('name');
        }else{
            $info['zhiye'] = '';
        }
        return $info;
    }
}
